import { nextTick, reactive, ref } from "vue";

export const useModalState = (
  validateFn: () => Promise<boolean>,
  resetFieldsFn: () => void
) => {
  const visible = ref(false);
  const title = ref("");
  const modalState = reactive({
    visible,
    title,
    onOk: async () => {
      const res = await validateFn();
      res && (visible.value = false);
    },
    async onCancel() {
      visible.value = false;
      await nextTick();
      resetFieldsFn();
    },
  });
  const openModal = (title: string) => {
    modalState.title = title;
    modalState.visible = true;
  };
  return {
    modalState,
    openModal,
  };
};

// export const useModalStateNew = (modalRef, rulesRef) => {
//   useFo
//
// };
